library(shiny)
library(tidyverse)
library(readxl)
library(knitr)
library(DT)
# Define UI for data upload app ----
ui <- fluidPage(
    
    # App title ----
    titlePanel("计算综测分小程序"),
    
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
        
        # Sidebar panel for inputs ----
        sidebarPanel(
            
            # Input: Select a file ----
            fileInput("file1", "选Excel文件",
                      multiple = FALSE),
            # Input: Select number of rows to display ----
            radioButtons("disp", "选择",
                         choices = c(原始数据 = "原始数据",
                                         结果 = "结果"),
                         selected = "head"),
            numericInput("纪律分", "纪律分:", 10),
            numericInput("突出分", "突出分:", 10),
            selectInput('学年',"学年:",c('2016-2017','2017-2018','2018-2019','2019-2020','2020-2021'))
            
        ),
        
        # Main panel for displaying outputs ----
        mainPanel(
            
            # Output: Data file ----
            dataTableOutput("contents")
            
        )
        
    ),
)

# Define server logic to read selected file ----
server <- function(input, output) {
    YourScore<- function(your_excel,your_Daily_point=0,your_outstanding_point=0,
                         your_period='2019-2020') {
        your_Daily_point=your_Daily_point*0.15
        your_outstanding_point=your_outstanding_point*0.1
        your_excel%>% select(-1) %>% 
            mutate(成绩=if_else(成绩=='优',90,if_else(
                成绩=='良',80,if_else(成绩=='中',70,if_else(成绩==
                                                            '及格',60,if_else(
                                                                成绩=='不及格',50,
                                                                as.numeric(成绩)
                                                            )))
            ))) %>% filter(!课程性质  
                           %in% c('通识类选修课程',
                                  '集中性实践教学环节')) %>% 
            filter(!str_detect(课程名称,'大学体育')) %>% 
            filter(学年==your_period)%>%#学年设置 
            summarise(总成绩=((sum(成绩*学分)/sum(学分))*0.75
                         +your_outstanding_point+
                             your_Daily_point),
                         突出分折算=your_outstanding_point,
                         纪律分折算=your_Daily_point,
                         学业分折算=((sum(成绩*学分)/sum(学分))*0.75)) %>% t() 
            
                
        
    }
    output$contents <- renderDataTable({
        
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, head of that data file by default,
        # or all rows if selected, will be shown.
        
        req(input$file1)
        
        # when reading semicolon separated files,
        # having a comma separator causes `read.csv` to error
        tryCatch(
            {
                df <- read_excel(input$file1$datapath)
            },
            error = function(e) {
                # return a safeError if a parsing error occurs
                stop(safeError(e))
            }
        )
        
        if(input$disp == "原始数据") {
            return(df)
        }
        else {
            return(YourScore(df,input$纪律分,input$突出分,your_period = input$学年))
        }
        
    })
    
}

# Create Shiny app ----
shinyApp(ui, server)